package Teacher.Assessment;

import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.GregorianCalendar;

import javax.swing.*;

import Database.*;
import Exception.*;
import General.*;
import GenericMethod.*;

public class AssessmentReportFrame extends JFrame
{
	private Container container;
	private JPanel centralPanel;
	private JButton buttonClose;
	private JLabel 	labelBackground,
					labelExamName, labelExamType, labelExamDate,
					labelHeaderStudent, labelStudentMatricule, labelStudentFirstname, labelStudentLastname,
					labelHeaderAssessment, labelAssessStartTime, labelAssessFinishTime, labelAssessPauseTimer, labelAssessTotalTime, labelAssessPenaltyPercentage,
					labelTraitmentTime;
	private JTextField 	fieldStudentId, fieldStudentFirstname, fieldStudentLastname,
						fieldAssessStartTime, fieldAssessFinishTime, fieldAssessPauseTimer, fieldAssessTotalTime, fieldAssessPenaltyPercentage;
	private String examinationName, examinationType, examinationDate, studentFirstname, studentLastname, assessStartTime, assessFinishTime, assessTotalTime;
	private int studentID, assessPauseTimer, assessPenaltyPercentage;
	private long traitmentTime;
	private Connection connection;
	private int idExamination, idStudent;
	private SetDimension setDimension;
	public AssessmentReportFrame(CallDB callDB, Connection connection, int idExamination, int idStudent) throws LoadDriverException, CheckDataException
	{
		super("Assessment report"); //Set the title
		
		setDimension = new SetDimension();
		setDimension.setBoundFrame(this, 440, 430);
		
		this.connection = connection;
		this.idExamination = idExamination;
		this.idStudent= idStudent;
				
		//Window Listener
		MyWindowListener windowListener = new MyWindowListener();
		this.addWindowListener(windowListener);
		
		//Container
		container = new Container();
		container.setLayout(new BorderLayout());
		container = getContentPane();
		
		//Creation du panneau
		centralPanel = new JPanel();
		centralPanel.setLayout(null);
		
		//Ajout du panneau
		container.add(centralPanel, BorderLayout.CENTER);
		
		//Recuperation des infos du rapport
		retrieveInformations();
		
		createComponent();
		showComponent();
		addListeners();
		
		setResizable(false);
		setVisible(true); //Make the window visible
	}

	private void createComponent()
	{
		//Examination information
		labelExamName = new JLabel(examinationName);
		labelExamName.setFont(new Font("Candara", Font.BOLD, 18));
		labelExamName.setBounds(20,20,350,27);
		
		labelExamType = new JLabel(examinationType);
		labelExamType.setFont(new Font("Candara", 0, 15));
		labelExamType.setBounds(50,40,100,27);
		
		labelExamDate = new JLabel("[" + examinationDate + "]");
		labelExamDate.setFont(new Font("Candara", 0, 15));
		labelExamDate.setBounds(150,40,100,27);
		
		//Student information
		labelHeaderStudent = new JLabel("Student informations");
		labelHeaderStudent.setFont(new Font("Candara", Font.BOLD, 15));
		labelHeaderStudent.setBounds(20,80,350,27);
		
		labelStudentMatricule = new JLabel("Matricule :");
		labelStudentMatricule.setFont(new Font("Candara", 0, 15));
		labelStudentMatricule.setBounds(50,107,70,27);
		fieldStudentId = new JTextField();
		fieldStudentId.setText("   " + String.valueOf(studentID));
		fieldStudentId.setBounds(130,107,80,27);
		
		labelStudentFirstname = new JLabel("Firstname :");
		labelStudentFirstname.setFont(new Font("Candara", 0, 15));
		labelStudentFirstname.setBounds(50,137,70,27);
		fieldStudentFirstname = new JTextField();
		fieldStudentFirstname.setText("   " + studentFirstname);
		fieldStudentFirstname.setBounds(130,137,220,27);
		
		labelStudentLastname = new JLabel("Lastname :");
		labelStudentLastname.setFont(new Font("Candara", 0, 15));
		labelStudentLastname.setBounds(50,167,70,27);
		fieldStudentLastname = new JTextField();
		fieldStudentLastname.setText("   " + studentLastname);
		fieldStudentLastname.setBounds(130,167,220,27);
		
		//Assessment information
		labelHeaderAssessment = new JLabel("Assessment informations");
		labelHeaderAssessment.setFont(new Font("Candara", Font.BOLD, 15));
		labelHeaderAssessment.setBounds(20,207,350,27);
		
		labelAssessStartTime = new JLabel("Start time :");
		labelAssessStartTime.setFont(new Font("Candara", 0, 15));
		labelAssessStartTime.setBounds(50,234,80,27);
		fieldAssessStartTime = new JTextField();
		fieldAssessStartTime.setText("   " + assessStartTime);
		fieldAssessStartTime.setBounds(130,234,80,27);
		
		labelAssessFinishTime = new JLabel("Finish time :");
		labelAssessFinishTime.setFont(new Font("Candara", 0, 15));
		labelAssessFinishTime.setBounds(230,234,80,27);
		fieldAssessFinishTime = new JTextField();
		fieldAssessFinishTime.setText("   " + assessFinishTime);
		fieldAssessFinishTime.setBounds(310,234,80,27);
		
		labelAssessPauseTimer = new JLabel("Pause :");
		labelAssessPauseTimer.setFont(new Font("Candara", 0, 15));
		labelAssessPauseTimer.setBounds(50,264,80,27);
		fieldAssessPauseTimer = new JTextField();
		fieldAssessPauseTimer.setText("   " + assessPauseTimer + " sec");
		fieldAssessPauseTimer.setBounds(130,264,80,27);
		
		labelAssessTotalTime = new JLabel("Total :");
		labelAssessTotalTime.setFont(new Font("Candara", 0, 15));
		labelAssessTotalTime.setBounds(230,264,80,27);
		fieldAssessTotalTime = new JTextField();
		fieldAssessTotalTime.setText("   " + assessTotalTime);
		fieldAssessTotalTime.setBounds(310,264,80,27);
		
		labelAssessPenaltyPercentage = new JLabel("Penalty :");
		labelAssessPenaltyPercentage.setFont(new Font("Candara", 0, 15));
		labelAssessPenaltyPercentage.setBounds(50,294,80,27);
		fieldAssessPenaltyPercentage = new JTextField();
		fieldAssessPenaltyPercentage.setText("   " + assessPenaltyPercentage + " %");
		fieldAssessPenaltyPercentage.setBounds(130,294,80,27);
		
		//Traitment & Button
		labelTraitmentTime = new JLabel("");
		labelTraitmentTime.setFont(new Font("Candara", 1, 12));
		labelTraitmentTime.setBounds(20, 360, 200, 27);
		labelTraitmentTime.setText("Report generated in " + traitmentTime + " msec");
		
		buttonClose = new JButton("Close");
		buttonClose.setBounds(270,360,120,30);
		
		labelBackground = new JLabel();
		labelBackground.setIcon(new ImageIcon("image/Bg-Report.jpg"));
		labelBackground.setBounds(0, 0, 440, 430);
		
		applyFieldsDesign();
	}
	
	private void showComponent()
	{
		//Examination information
		centralPanel.add(labelExamName);
		centralPanel.add(labelExamType);
		centralPanel.add(labelExamDate);
		
		//Student information
		centralPanel.add(labelHeaderStudent);
		centralPanel.add(labelStudentMatricule);
		centralPanel.add(fieldStudentId);
		centralPanel.add(labelStudentFirstname);
		centralPanel.add(fieldStudentFirstname);
		centralPanel.add(labelStudentLastname);
		centralPanel.add(fieldStudentLastname);
		
		//Assessment information
		centralPanel.add(labelHeaderAssessment);
		centralPanel.add(labelAssessStartTime);
		centralPanel.add(fieldAssessStartTime);
		centralPanel.add(labelAssessFinishTime);
		centralPanel.add(fieldAssessFinishTime);
		centralPanel.add(labelAssessPauseTimer);
		centralPanel.add(fieldAssessPauseTimer);
		centralPanel.add(labelAssessTotalTime);
		centralPanel.add(fieldAssessTotalTime);
		centralPanel.add(labelAssessPenaltyPercentage);
		centralPanel.add(fieldAssessPenaltyPercentage);
		
		//Button
		centralPanel.add(labelTraitmentTime);
		centralPanel.add(buttonClose);
		
		//Background
		centralPanel.add(labelBackground);
	}

	private void retrieveInformations() throws LoadDriverException, CheckDataException
	{
		//Recuperation du temps en milliseconde, avant la requete
		long before = new GregorianCalendar().getTimeInMillis();
		
		//Application de la requete
		MyTableModel resultModel = new AssessmentMgmt(connection).selectElementForOneStudent(idExamination, idStudent);
		
		//Calcul du temps de traitement
		traitmentTime = (new GregorianCalendar().getTimeInMillis() - before);
		
		//Assignation des valeurs
		int cpt = 0;
		
		examinationName  = (String)resultModel.getValueAt(0, cpt); cpt++;
		examinationType  = (String)resultModel.getValueAt(0, cpt); cpt++;
		examinationDate  = (String)resultModel.getValueAt(0, cpt); cpt++;
		studentID		 = (int)resultModel.getValueAt(0, cpt); cpt++;
		studentFirstname = (String)resultModel.getValueAt(0, cpt); cpt++;
		studentLastname  = (String)resultModel.getValueAt(0, cpt); cpt++;
		
		assessStartTime	= "NOT DONE!";
		assessFinishTime = "NOT DONE!";
		assessPauseTimer = 999;
		assessTotalTime = "NOT DONE!";
		assessPenaltyPercentage = 999;
		
		if((String)resultModel.getValueAt(0, cpt) != null) //Si pas commence
		{
			assessStartTime = (String)resultModel.getValueAt(0, cpt);
			cpt++;
			
			if((String)resultModel.getValueAt(0, cpt) != null)
			{
				assessFinishTime = (String)resultModel.getValueAt(0, cpt); cpt++;
				assessPauseTimer = (int)   resultModel.getValueAt(0, cpt); cpt++;
				assessTotalTime	 = (String)resultModel.getValueAt(0, cpt); cpt++;
				assessPenaltyPercentage = 100 - (int)resultModel.getValueAt(0, cpt);
			}
		}
	}
	
	private void applyFieldsDesign()
	{
		fieldStudentId.setEditable(false);
		fieldStudentId.setBackground(Color.WHITE);
		
		fieldStudentFirstname.setEditable(false);
		fieldStudentFirstname.setBackground(Color.WHITE);
		
		fieldStudentLastname.setEditable(false);
		fieldStudentLastname.setBackground(Color.WHITE);
		
		fieldAssessStartTime.setEditable(false);
		fieldAssessStartTime.setBackground(Color.WHITE);
		
		fieldAssessFinishTime.setEditable(false);
		fieldAssessFinishTime.setBackground(Color.WHITE);
		
		fieldAssessPauseTimer.setEditable(false);
		fieldAssessPauseTimer.setBackground(Color.WHITE);
		
		fieldAssessTotalTime.setEditable(false);
		fieldAssessTotalTime.setBackground(Color.WHITE);
		
		fieldAssessPenaltyPercentage.setEditable(false);
		fieldAssessPenaltyPercentage.setBackground(Color.WHITE);
	}
	
	private void addListeners()
	{
		MyActionListener listnerAction = new MyActionListener();
		buttonClose.addActionListener(listnerAction);
	}
	
	private class MyActionListener implements ActionListener{
		public void actionPerformed(ActionEvent arg0){
			if(arg0.getSource() == buttonClose)
				dispose();}}	
	private class MyWindowListener extends WindowAdapter{
		public void windowClosing(WindowEvent arg0){
			dispose();}}
}
